package com.example.aboutme

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.example.aboutme.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    // MainActivity的绑定对象。
    // 对象的名称是从activity 或fragment的名称派生的
    private lateinit var binding: ActivityMainBinding

    // Instance of MyName data class
    private val myName = MyName("Bruce Lee")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        //设置myName变量的值为layout文件中定义的
        binding.myName = myName
        binding.doneButton.setOnClickListener {
            addNickName(it)
        }
    }

    fun addNickName(view: View){

        binding.apply {
            //设置nicknameView显示的文本
            myName?.nickname = "你好，" + nicknameEdit.text.toString()

            //使所有绑定表达式失效，并请求重新绑定以刷新UI
            invalidateAll()

            //隐藏nicknameEdit
            nicknameEdit.visibility = View.GONE

            //隐藏Button
            view.visibility = View.GONE

            //显示nicknameView
            nicknameText.visibility = View.VISIBLE
        }

        //隐藏输入法
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }
}